package com.xcc.mota.sprite;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

/**
 * 地图中的精灵
 * Created by Administrator on 2018/1/19.
 */
public class MapSprite extends BaseSprite {
    private int mapX, mapY;
    private int mapIdx;
    private Bitmap bitmap[][];
    private int bitmapIdx;
    private int bitmapToIdx;

    public void setBitmapIdx(int bitmapIdx) {
        this.bitmapIdx = bitmapIdx;
    }

    public void setBitmap(Bitmap[][] bitmap) {
        this.bitmap = bitmap;
    }

    public MapSprite setMapIdx(int mapIdx) {
        this.mapIdx = mapIdx;
        mapY = mapIdx / 100;
        mapX = mapIdx % 100;
        setX(mapX * 32);
        setY(mapY * 32);
        return this;
    }

    public int getMapX() {
        return mapX;
    }

    public int getMapY() {
        return mapY;
    }

    public void onDraw(Canvas canvas, Paint paint) {
        RectF rectF = getRectF();
        if (rectF == null) {
            setRectF(new RectF(x, y, x + 32, y + 32));
        }
        if (bitmap != null) {
            Bitmap[] bitmaps = bitmap[bitmapIdx];
            if (bitmaps != null) {
                Bitmap bitmap = bitmaps[bitmapToIdx];
                if (bitmap != null) {
                    rectF = getRectF();
                    canvas.drawBitmap(bitmap, rectF.left, rectF.top, null);
                }

                bitmapToIdx++;
                if (bitmapToIdx >= bitmaps.length) bitmapToIdx = 0;
            }
        }
    }
}
